package com.it.gw.modules.station.adapter

import EquimentVo
import EquimentVoList
import android.annotation.SuppressLint
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.it.common.utils.isNotEmpty
import com.it.gw.databinding.ItemEquimentBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.station.activity.GQStationReportDetailActivity


class EquimentDetailAdapter(
    var context: GQStationReportDetailActivity,
    var leftListener: ((EquimentVo?) -> Unit)? = null,
    var rigjtListener: ((EquimentVo?) -> Unit)? = null,
    var queDealEnable:Boolean? = true,
    var datas: List<EquimentVoList>
) : RecyclerView.Adapter<EquimentDetailAdapter.ViewHolder>() {
    companion object {
        const val TAG = "EquimentDetailAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemEquimentBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val data = datas[position]
        Log.d(TAG, "onBindViewHolder: $data")
        holder.binding.equipment1.text = data.list?.get(0)?.equipment
        if(queDealEnable==true){
            if( data.list?.get(1)?.equipment!=null){
                holder.binding.equipment2.text = data.list?.get(1)?.equipment!!.split("_")[0]
            }
        }
        else{
            if (data.list?.get(1)?.equipment != null) {
                runCatching {
                    context.equipment(data.id!!, resp = { d ->
                        ProgressDialog.hide()
                        Log.d(TAG, "onBindViewHolder========: ${data.id} ${d.data}")
                        if (d.data.isNotEmpty()) {
                            val model = "${d.data?.get(1)!!.type!!.split("_")[1]}"
                            val code = data.list?.get(1)?.equipment
                            context.gqStationViewModel.getDictName(model) {
                                ProgressDialog.hide()
                                Log.d(TAG, "getDictName: getDictName========= ${code}")
                                Log.d(TAG, "getDictName: getDictName========= ${it.data}")
                                if (it.data.isNotEmpty()) {
                                    var vo = it.data?.find { it.dataCode == code }
                                    holder.binding.equipment2.text = vo?.dataName

                                }

                            }
                        }
                    })
                }


            }
        }






        holder.binding.equipment1.setOnClickListener {
            leftListener?.invoke(data.list?.get(0))
        }
        holder.binding.equipment2.setOnClickListener {

            Log.d(TAG, "onBindViewHolder: rigjtListener ${data.list?.get(1)}")
            rigjtListener?.invoke(data.list?.get(1))
        }


    }

    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemEquimentBinding) :
        RecyclerView.ViewHolder(binding.root)
}